﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DatabaseProject
{
    public partial class BookManager : Form
    {
        public BookManager()
        {
            InitializeComponent();
        }
        string connectionString = @"Server=.\SQLEXPRESS;Database=Book_saling_management;Trusted_Connection=True;";
        public void initBooks()
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                string query = "select * from Book";
      
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, conn);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);

                dgvBooks.DataSource = dataSet.Tables[0].DefaultView;
            }
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initBooks();
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void 版次_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string id = dgvBooks.CurrentRow.Cells["ISBN"].Value.ToString();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                string query = "update Book set ISBN = '{0}' ,Bookname='{1}',Publisher='{2}' ,Publish_date='{3}',Version='{4}',Word_count='{5}',Packaging='{6}',Price='{7}' where ISBN = '{8}';";
                query = string.Format(query, textBookID.Text, textBookname.Text, textAuthor.Text, textPrice.Text, textBookshelf.Text, textPublisher.Text, textStock.Text, id);
                SqlCommand command = new SqlCommand(query, conn);

                command.ExecuteNonQuery();
                initBooks();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                string query = "insert into Book values( '{0}' ,'{1}','{2}' ,'{3}','{4}','{5}','{6}');";
                query = string.Format(query, textBookID.Text, textBookname.Text, textAuthor.Text, textPrice.Text, textBookshelf.Text, textPublisher.Text,textStock.Text);
                SqlCommand command = new SqlCommand(query, conn);

                command.ExecuteNonQuery();
                initBooks();
                
                foreach (Control control in this.Controls)
                {
                    if (control is TextBox)
                    {
                        ((TextBox)control).Clear();
                    }
                }
            }

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string id = dgvBooks.CurrentRow.Cells["ISBN"].Value.ToString();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();


                string query = "delete from Book where ISBN = '{0}'";
                query = string.Format(query, id);
                SqlCommand command = new SqlCommand(query, conn);

                command.ExecuteNonQuery();
                initBooks();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                string query = "select * from Book where Bookname = '{0}'";
                query = string.Format(query, textBookname.Text);
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, conn);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);

                dgvBooks.DataSource = dataSet.Tables[0].DefaultView;
                initBooks();
            }
        }

        private void 刷新_Click(object sender, EventArgs e)
        {
            initBooks();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}